
package threads.volatiletest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SNRunner
{

	private static int threadsNumber = 15;

	public static void main(String[] args) throws ExecutionException
	{

		List<Integer> serialNumbers = new ArrayList<Integer>();

		ExecutorService execService = Executors.newCachedThreadPool();

		List<Future<SNContainer>> results = new ArrayList<Future<SNContainer>>();

		List<SNProvider> tasks = new ArrayList<SNProvider>();

		for (int i = 0; i < threadsNumber; i++)
		{
			tasks.add(new SNProvider());
		}

		while (true)
		{
			try
			{
				results = execService.invokeAll(tasks);

				for (Future<SNContainer> future : results)
				{
					SNContainer container = future.get();

					if (!serialNumbers.contains(container.getSerialNumber()))
					{
						serialNumbers.add(container.getSerialNumber());
					}
					else
					{
						System.out.println("Duplicate is " + container.getSerialNumber());
						System.exit(0);
					}
				}
			}
			catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

}
